// 
// Bachelor of Software Engineering
// Media Design School
// Auckland
// New Zealand
//
// (c) 2005 - 2014 Media Design School
//
// File Name	: AlienComponent.cpp
// Desrciption	: CAlienComponent implementation file.
// Author		: Hayden Asplet.
// Mail			: hayden.asplet@mediadesignschool.com
//

// PCH
#include "SpaceInvadersStd.h"

// Library Includes

// This Include
#include "AlienComponent.h"

// Local Includes

CAlienComponent::CAlienComponent()
	:m_iWorth(0),
	m_fInitialBulletVelocity(0.0f),
	m_fBulletAcceleration(0.0f)
{

}

CAlienComponent::~CAlienComponent()
{

}

bool CAlienComponent::VInitialize(TiXmlElement* _pXmlData)
{
	TiXmlElement* pWorth = _pXmlData->FirstChildElement("Worth");

	if(pWorth) {
		// Get the worth amount from the xml.
		std::string value = pWorth->FirstChild()->Value();

		// Convert to integer.
		m_iWorth = atoi(value.c_str());
	}
	else {
	// No worth element found in xml.
		// An alien component needs a worth value.
		return false;
	}

	TiXmlElement* pInitialBulletVelocity = _pXmlData->FirstChildElement("InitialBulletVelocity");

	if(pInitialBulletVelocity) {
		// Get the bullet velocity amount from the xml.
		std::string value = pInitialBulletVelocity->FirstChild()->Value();

		// Convert to integer.
		m_fInitialBulletVelocity = static_cast<float>(atof(value.c_str()));
	}
	else {
	// No initial bullet velocity found in xml.
		// An alien component needs an initial bullet velocity.
		return false;
	}

	TiXmlElement* pBulletAcceleration = _pXmlData->FirstChildElement("BulletAcceleration");

	if(pBulletAcceleration) {
		// Get the bullet velocity amount from the xml.
		std::string value = pBulletAcceleration->FirstChild()->Value();

		// Convert to integer.
		m_fBulletAcceleration = static_cast<float>(atof(value.c_str()));
	}
	else {
	// No initial bullet velocity found in xml.
		// An alien component needs an initial bullet velocity.
		m_fBulletAcceleration = 0.0f;
	}

	// Success.
	return true;
}

void CAlienComponent::VPostInitialize()
{
	// Nothing to post initialize.
}

void CAlienComponent::VUpdate(float _fDeltaTime)
{
	// Nothing to update.
}

void CAlienComponent::FireBullet()
{
	// Create a bullet.
	CGameObject* pBullet = g_pApp->m_pGame->CreateGameObject("GameObjects\\AlienBullet.xml");

	if(pBullet) {
	// Did the bullet create successfully?
		// Obtain the required components to fire a bullet.
		shared_ptr<CPositionComponent> pBulletPosition = pBullet->GetComponent<CPositionComponent>();
		shared_ptr<CAnimationComponent> pBulletAnimation = pBullet->GetComponent<CAnimationComponent>();
		shared_ptr<CPhysicsComponent> pAlienPhysics = m_pOwner->GetComponent<CPhysicsComponent>();

		if(pBulletPosition && pAlienPhysics) {
		// Do all these components exist?
			CRect alienWorldBox = pAlienPhysics->GetWorldBoundingBox();
			CPoint bulletPosition(alienWorldBox.m_iX + alienWorldBox.m_iWidth / 2, alienWorldBox.GetBottom());

			pBulletPosition->SetPosition(bulletPosition);

			shared_ptr<CEvtDataMoveObject> pEvent(new CEvtDataMoveObject(pBullet->GetId(), false, m_fBulletAcceleration, m_fInitialBulletVelocity));
			CEventManager::GetInstance()->QueueEvent(pEvent);
		}

		if(pBulletAnimation) {
			// Select a random animation.
			int iRandom = GetRandomNumber(1, pBulletAnimation->GetAmountOfAnimations() + 1);
			
			std::string animationName = "Anim" + std::to_string(static_cast<long long>(iRandom));

			// Play the animation.
			pBulletAnimation->PlayAnimation(animationName, 5, true);
		}
	}
	else {
	// Bullet couldn't be created successfully.
		std::cerr << "WARNING: Bullet not created when firing one from a alien." << std::endl;
	}
}

int CAlienComponent::GetWorth() const
{
	return m_iWorth;
}

void CAlienComponent::SetWorth(int _iWorth)
{
	m_iWorth = _iWorth;
}

TStrongObjectComponentPtr CreateAlienComponent()
{
	return TStrongObjectComponentPtr(new CAlienComponent());
}